package com.newtouch.xcd.cms.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.newtouch.xcd.cms.entity.Benefit;
import com.newtouch.xcd.cms.entity.Distribution;
import com.newtouch.xcd.cms.mapper.DistributionMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newtouch.xcd.cms.service.BenefitService;
import com.newtouch.xcd.cms.service.DistributionService;
import com.newtouch.xcd.common.constants.CrmDict;
import com.newtouch.xcd.common.utils.EnumTools;
import com.newtouch.xcd.common.utils.StrUtils;
import com.newtouch.xcd.mft.vo.BeneficiaryItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dongyuefeng
 * @since 2020-06-11
 */
@Component
@Service
public class DistributionServiceImpl extends ServiceImpl<DistributionMapper, Distribution> implements DistributionService {

    @Autowired
    private BenefitService benefitService;

    /**
     * 根据利益编号ID查找数据
     *
     * @param benefitId 利益编号ID
     * @return
     */
    public List<Distribution> queryByBenefitId(String benefitId) {

        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("benefit_id", benefitId);

        return this.list(queryWrapper);
    }

    /**
     * 根据合同号查找受益人列表，去重，返回的数据按受益人类别排序
     *
     * @param contractId 合同号ID
     * @return
     */
    @Override
    public List<BeneficiaryItem> queryBeneficiaryItemByContractId(String contractId) {

        List<BeneficiaryItem> result = new ArrayList<>();
        Set<String> beneficiaryNameSet = new HashSet<>();

        List<Benefit> benefits = this.benefitService.queryByContractId(contractId);

        if (CollUtil.isEmpty(benefits)) {
            return result;
        }

        for (Benefit benefit : benefits) {
            String benefitId = Convert.toStr(benefit.getBenefitId());

            //获得收益明细
            List<Distribution> distributionList = this.queryByBenefitId(benefitId);

            if (CollUtil.isEmpty(distributionList)) {
                continue;
            }

            for (Distribution distribution : distributionList) {
                String name = distribution.getContactsName();
                if (beneficiaryNameSet.contains(name)) {
                    continue;
                }

                beneficiaryNameSet.add(name);
                String contractsType = Convert.toStr(distribution.getContactsType());
                String beneficiaryType = EnumTools.getByCode(CrmDict.ContactsType.class, contractsType).getMessage();
                BeneficiaryItem beneficiaryItem = new BeneficiaryItem(beneficiaryType, name);
                result.add(beneficiaryItem);
            }
        }

        return result;

    }

    /**
     * 根据合同号，返回利益分配方案明细
     *
     * @param contractId 合同号ID
     * @return
     */
    @Override
    public List<Distribution> queryDistributionByContractId(String contractId) {

        List<Distribution> result = new ArrayList<>();

        List<Benefit> benefits = this.benefitService.queryByContractId(contractId);

        if (CollUtil.isEmpty(benefits)) {
            return result;
        }

        for (Benefit benefit : benefits) {
            String benefitId = Convert.toStr(benefit.getBenefitId());

            //获得收益明细
            List<Distribution> distributionList = this.queryByBenefitId(benefitId);

            if (CollUtil.isEmpty(distributionList)) {
                continue;
            }

            //做个特殊处理，如果分配项目为空，则根据分配项目类型，为分配项目填上值。
            for (Distribution distribution : distributionList) {

                String project = distribution.getFrequency();

                if (StrUtils.isBlank(project)) {

                    String projectType = Convert.toStr(distribution.getFrequencyType());

                    try {
                        project = EnumTools.getByCode(CrmDict.FrequencyType.class, projectType).getMessage();
                    } catch (Exception e) {
                        project = "其他项目";
                    }
                    distribution.setFrequency(project);
                }

                result.add(distribution);
            }
        }

        return result;

    }

    /**
     * 根据 合同号+受益人+分配的项目，返回利益分配详情
     *
     * @param contractId          合同号ID
     * @param beneficiary
     * @param distributionProject
     * @return
     */
    @Override
    public Distribution queryBenefitDistributionByContractIdAndBeneficiaryAndDistributionProject(String contractId, String beneficiary, String distributionProject) {

        List<Distribution> distributionList = this.queryDistributionByContractId(contractId);

        distributionList = distributionList.stream().filter(o -> o.getContactsName().equals(beneficiary)).filter(o -> o.getFrequency().equals(distributionProject)).collect(Collectors.toList());

        if (CollUtil.isEmpty(distributionList)) {
            return null;
        }

        return distributionList.get(0);
    }

    /**
     * 检查合同记录
     * @return
     */
    @Override
    public List<Distribution> checkDistribution(){
        return baseMapper.checkDistribution();
    }
}
